﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ES_Project.Model;
using MySql.Data.MySqlClient;
using System.Web.Configuration;
using System.Data;

namespace ES_Project.Pages
{

    public partial class WebForm177 : System.Web.UI.Page
    {
        string StrConn = WebConfigurationManager.ConnectionStrings["baseessystem"].ToString();
        MySqlCommand objCmd = new MySqlCommand();

        #region "Page Load"
        protected void Page_Load(object sender, EventArgs e)
        {


            
            if (!Page.IsPostBack)
            {
                requestEmpId.Text = Request.QueryString["emp_id"].ToString();
                //queryShow();
                BindData();
                //sumSalary();
                //BindDataPay();
            }
            
        }
        #endregion

        
        //#region "search PayCode"
        //protected void CMD_PayCode_Click(object sender, EventArgs e)
        //{

        //    using (essystemEntities context = new essystemEntities())
        //    {
        //        List<pay_code_mast> crm2 = context.pay_code_mast.Where("it.[pay_code]= '" + payCode.Text + "' ").ToList();
        //        if (crm2.Count() == 1)
        //        {
        //            pay_code_mast em = context.pay_code_mast.Where("it.[pay_code]='" + payCode.Text + "'").First();
        //            if (em.pay_code != null)
        //            {
        //                payDesc.Text = em.pay_desc;
        //                payId.Text = Convert.ToString(em.pay_id);

        //            }
        //        }
        //    }
        //}
        //#endregion

        #region "BindData"
        void BindData()
        {
            MySqlConnection Conn = new MySqlConnection(StrConn);
            
            String strSQL;
            

           // CMD_INSERT.Enabled = true;
            CMD_EDIT.Enabled = false;
            strSQL = "select em.emp_id,em.emp_code, em.emp_name,em.emp_type, em.emp_position,pcm.pay_id " +
                        " , pcm.pay_desc  " +
                        " , pcm.pay_month_amt, pcm.pay_time_amt, pcm.pay_hour_amt ,pay_rate , pay_rate1 , pcm.pay_code" +
                        " , IF(pcm.pay_type = 'm', 'รายเดือน', IF(pcm.pay_type = 't', 'รายครั้ง',IF(pcm.pay_type = 'h', 'รายชั่วโมง',IF(pcm.pay_type = 'r', '%ลงทะเบียน','รายเดือน+%ลงทะเบียน' )))) AS pay_type " +
                        
                        "from emp_mast em "+
                        "left outer join pay_code_mast pcm " +
                        "on pcm.pay_id = em.pay_id "+
                        "where em.emp_id = '" + Convert.ToInt32(Request.QueryString["emp_id"]) + "' ";
                        //"where em.emp_id = ' 1 ' ";
                        //" and em.pay_id != '" + " " + "' ";
                       // " and crm.cls_reg_id != '"+" "+"' ";



            Conn.Open();
            
            
            
            MySqlDataAdapter da4 = new MySqlDataAdapter(strSQL, Conn);
            DataSet ds4 = new DataSet();
            da4.Fill(ds4, "emp_mast");
            int _row = ds4.Tables[0].Rows.Count;
            int i = 0;
            if (_row > i )
            {
                
                if (!((string)ds4.Tables[0].Rows[i]["pay_id"].ToString()).Equals(""))
                {
                    CMD_INSERT.Enabled = false;
                    CMD_EDIT.Enabled = true;
                    GridView12.DataSource = ds4.Tables["emp_mast"];
                    GridView12.DataBind();
                    da4.Fill(ds4);
                    
                    Conn.Close();
                    Conn = null;

                    
                }
                else 
                {
                    CMD_INSERT.Enabled = true;
                    CMD_EDIT.Enabled = false;
                    Conn.Close();
                    Conn = null;
                }

                


            }
        }
        #endregion

        //#region "BindDataPayCodeMast"
        //void BindDataPay()
        //{
        //    MySqlConnection Conn = new MySqlConnection(StrConn);

        //    String strSQL;


        //    // CMD_INSERT.Enabled = true;
        //    CMD_EDIT.Enabled = false;
        //    strSQL = " select pcm.pay_id, pcm.pay_desc, pcm.pay_salary, pcm.emp_id, pcm.for_month, pcm.for_year, pcm.pay_code,  "+
        //             " IF(pcm.pay_type = 0, 'รายเดือน', IF(pcm.pay_type = 1, 'รายวัน','รายชม.' )) AS pay_type" +
        //             " from pay_code_mast pcm "+
        //             " where pcm.emp_id = '" + Convert.ToInt32(Request.QueryString["emp_id"]) + "' ";
        //    //"where em.emp_id = ' 1 ' ";
        //    //" and em.pay_id != '" + " " + "' ";
        //    // " and crm.cls_reg_id != '"+" "+"' ";



        //    Conn.Open();
        //    //MySqlDataReader dtReader;
        //    //objCmd = new MySqlCommand(strSQL, Conn);
        //    //dtReader = objCmd.ExecuteReader();

        //    ////*** BindData to GridView ***//
        //    //GridView11.DataSource = dtReader;
        //    //GridView11.DataBind();


        //    MySqlDataAdapter da3 = new MySqlDataAdapter(strSQL, Conn);
        //    DataSet ds3 = new DataSet();
        //    da3.Fill(ds3, "pay_code_mast");
        //    int _row = ds3.Tables[0].Rows.Count;
        //    int i = 0;
        //    if (_row > i)
        //    {
        //        if (!((string)ds3.Tables[0].Rows[i]["pay_code"].ToString()).Equals("") && Convert.ToInt32((string)ds3.Tables[0].Rows[i]["for_month"].ToString()) > 0 && Convert.ToInt32((string)ds3.Tables[0].Rows[i]["for_year"].ToString()) > 0)
        //        {
        //            GridView12.DataSource = ds3.Tables["pay_code_mast"];
        //            GridView12.DataBind();
        //            //da4.Fill(ds4);

        //            Conn.Close();
        //            Conn = null;


        //        }
        //        else
        //        {

        //            Conn.Close();
        //            Conn = null;
        //        }




        //    }


        //}
        //#endregion

        //#region "sum value for group of emp_id"
        //void sumSalary()
        //{
        //    MySqlConnection Conn = new MySqlConnection(StrConn);
        //    string strSQL;
        //    //strSQL = "SELECT cls_reg_code,cls_name FROM cls_register_mast     WHERE cls_reg_code ='" + TextBox22.Text + "'";
        //    strSQL = " select sum(ecm.emp_cls_sal) emp_cls_sal , sum(ecm.emp_cls_sal_hours) emp_cls_sal_hours, "+
        //             " sum(ecm.emp_cls_sal_day) emp_cls_sal_day , sum(ecm.emp_cls_sal_month) emp_cls_sal_month "+
        //             " from emp_cls_mast ecm "+
        //             " where ecm.emp_id = '" + Convert.ToInt32(Request.QueryString["emp_id"]) + "' " +
        //             " group by ecm.emp_id ";

        //    Conn.Open();
        //    MySqlDataAdapter da4 = new MySqlDataAdapter(strSQL, Conn);
        //    DataSet ds4 = new DataSet();
        //    da4.Fill(ds4);

        //    int _row = ds4.Tables[0].Rows.Count;
        //    int i = 0;
        //    if (_row > i)
        //    {


        //        paySalary.Text = (string)ds4.Tables[0].Rows[i]["emp_cls_sal_month"].ToString();
        //        paySalary0.Text = (string)ds4.Tables[0].Rows[i]["emp_cls_sal_day"].ToString();
        //        paySalary1.Text = (string)ds4.Tables[0].Rows[i]["emp_cls_sal_hours"].ToString();
        //        //TextBox38.Text = (string)ds4.Tables[0].Rows[i]["cls_reg_detail_id"].ToString();


        //    }
        //    else
        //    {
        //        paySalary.Text = "";

        //    }
        
        
        //}
        //#endregion

        //#region "queryShow data of 2 table ==> emp_mast && pay_code_mast"
        //void queryShow()
        //{
            
        //    MySqlConnection Conn = new MySqlConnection(StrConn);

        //    String strSQL;

        //    strSQL = "select em.emp_id,em.emp_code, em.emp_name, pcm.pay_id,pcm.pay_code, pcm.pay_desc " +
        //             "from emp_mast em left outer join pay_code_mast pcm " +
        //             "on em.pay_id = pcm.pay_id " +
        //             "where em.emp_id = '" + Convert.ToInt32(Request.QueryString["emp_id"]) + "' ";
        //             //"where em.emp_id = ' 1 ' ";
        //    Conn.Open();
        //    MySqlDataAdapter da4 = new MySqlDataAdapter(strSQL, Conn);
        //    DataSet ds4 = new DataSet();
        //    da4.Fill(ds4);
        //    Conn.Close();
        //    Conn = null;
        //    int _row = ds4.Tables[0].Rows.Count;
        //    int i = 0;
        //    if (_row > i)
        //    {
                
        //        emp_id.Text = Convert.ToString(ds4.Tables[0].Rows[i]["emp_id"]).ToString();
        //        if (ds4.Tables[0].Rows[i]["pay_id"] != null && (string)ds4.Tables[0].Rows[i]["pay_id"].ToString() != "")
        //        {
        //            CMD_INSERT.Enabled = false;
        //            CMD_EDIT.Enabled = true;
        //            payCode.Text = (string)ds4.Tables[0].Rows[i]["pay_code"].ToString();
        //            payDesc.Text = (string)ds4.Tables[0].Rows[i]["pay_desc"].ToString();

        //        }
        //        else
        //        {
        //            CMD_INSERT.Enabled = true;
        //            CMD_EDIT.Enabled = false ;
        //            payCode.Text = (string)ds4.Tables[0].Rows[i]["pay_code"].ToString();
        //            payDesc.Text = (string)ds4.Tables[0].Rows[i]["pay_desc"].ToString();
        //        }

        //    }
            
            
            
        
        //}
        //#endregion

        #region "clear value of textboxs"
        private void clear()
        {
            payDesc.Text = "";
            checkStatus.Text = "";
            payMonthAmt.Text = "";
            payHourAmt.Text = "";
            payTimeAmt.Text = "";
            payRate.Text = "";
            payRate1.Text = "";
            payCode.Text = "";
            payDesc.Text = "";
            
            
        }

        protected void CMD_CLEAR_Click(object sender, EventArgs e)
        {
            clear();
        }
        #endregion

        //#region "Save and Edit to add  pay_id value for emp_mast table"
        //protected void CMD_INSERT_Click(object sender, EventArgs e)
        //{
        //    using (ES_Project.Model.essystemEntities context = new ES_Project.Model.essystemEntities())
        //    {
        //        Button bt = (Button)sender;
        //        switch (bt.ID)
        //        {
        //            case "CMD_INSERT":
                        

        //                List<emp_mast> crm2 = context.emp_mast.Where("it.[emp_id]=" + Convert.ToInt32(Request.QueryString["emp_id"]) + " ").ToList();
        //                if (crm2.Count() == 1)
        //                {
        //                    emp_mast crmEdit = context.emp_mast.Where("it.[emp_id]=" + Convert.ToInt32(Request.QueryString["emp_id"]) + " ").First();
        //                    if (crmEdit != null)
        //                    {
        //                        //crmEdit.cls_course_time = clsCourseTime.Text;
        //                        //crmEdit.cls_course_amt = clsCourseAmt.Text;
        //                        crmEdit.pay_id = Convert.ToInt32(payId.Text);
        //                        //crmEdit.cls_course_no = clsCourseNo.Text;


        //                        if (context.SaveChanges() > 0)
        //                        {

        //                            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "Alert2", "alert('เพิ่มข้อมูลเรียบร้อยแล้ว')", true);
        //                            //GridView4.DataBind();
        //                            BindData();
        //                            GridView11.SelectedIndex = -1;
        //                            //CMD_INSERT.Enabled = true;
        //                            //CMD_EDIT.Enabled = false;
        //                            //clsRegCode.Text = "";
        //                            //clsName.Text = "";
        //                            payId.Text = "";
        //                            //clsCourseTime.Text = "";
        //                            //clsCourseAmt.Text = "";
        //                            //clsCourseNo.Text = "";

        //                        }
        //                    }
        //                }
        //                break;
        //            case "CMD_EDIT":

        //                List<emp_mast> crm1 = context.emp_mast.Where("it.[emp_id]=" + Convert.ToInt32(Request.QueryString["emp_id"]) + " ").ToList();
        //                if (crm1.Count() == 1)
        //                {
        //                    emp_mast crmEdit1 = context.emp_mast.Where("it.[emp_id]=" + Convert.ToInt32(Request.QueryString["emp_id"]) + " ").First();
        //                    if (crmEdit1 != null)
        //                    {
        //                        //crmEdit.cls_course_time = clsCourseTime.Text;
        //                        //crmEdit.cls_course_amt = clsCourseAmt.Text;
        //                        crmEdit1.pay_id = Convert.ToInt32(payId.Text);
        //                        //crmEdit.cls_course_no = clsCourseNo.Text;


        //                        if (context.SaveChanges() > 0)
        //                        {

        //                            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "Alert2", "alert('แก้ไขข้อมูลเรียบร้อยแล้ว')", true);
        //                            //GridView4.DataBind();
        //                            BindData();
        //                            GridView11.SelectedIndex = -1;
        //                            //CMD_INSERT.Enabled = true;
        //                            //CMD_EDIT.Enabled = false;
        //                            //clsRegCode.Text = "";
        //                            //clsName.Text = "";
        //                            payId.Text = "";
        //                            //clsCourseTime.Text = "";
        //                            //clsCourseAmt.Text = "";
        //                            //clsCourseNo.Text = "";

        //                        }
        //                    }
        //                }
        //                break;
        //        }
        //    }
        //}
        //#endregion

        //#region "Update data in gridview11"
        //protected void modUpdateCommand(Object sender, GridViewUpdateEventArgs e)
        //{
        //    string StrConn = WebConfigurationManager.ConnectionStrings["baseessystem"].ToString();
        //    MySqlConnection objConn = new MySqlConnection();
        //    MySqlCommand objCmd = new MySqlCommand();

        //    objConn = new MySqlConnection(StrConn);
        //    objConn.Open();
        //    string strSQL;
        //    //*** CustomerID ***//
        //    TextBox txtEditEmpClsHours = (TextBox)GridView11.Rows[e.RowIndex].FindControl("txtEditEmpClsHours");

        //    TextBox txtEditEmpClsTotalHours = (TextBox)GridView11.Rows[e.RowIndex].FindControl("txtEditEmpClsTotalHours");

        //    TextBox txtEditEmpClsSal = (TextBox)GridView11.Rows[e.RowIndex].FindControl("txtEditEmpClsSal");

        //    TextBox txtEditEmpClsSalMonth = (TextBox)GridView11.Rows[e.RowIndex].FindControl("txtEditEmpClsSalMonth");

        //    TextBox txtEditEmpClsSalDay = (TextBox)GridView11.Rows[e.RowIndex].FindControl("txtEditEmpClsSalDay");

        //    TextBox txtEditEmpClsSalHours = (TextBox)GridView11.Rows[e.RowIndex].FindControl("txtEditEmpClsSalHours");

        //    strSQL = "UPDATE emp_cls_mast SET "+
        //        "  emp_cls_hours = '" + txtEditEmpClsHours.Text + "' " +
        //        " ,emp_cls_total_hours = '" + txtEditEmpClsTotalHours.Text + "' " +
        //        " ,emp_cls_sal = '" + txtEditEmpClsSal.Text + "' " +
        //        " ,emp_cls_sal_month = '" + txtEditEmpClsSalMonth.Text + "' " +
        //        " ,emp_cls_sal_day = '" + txtEditEmpClsSalDay.Text + "' " +
        //        " ,emp_cls_sal_hours = '"+ txtEditEmpClsSalHours.Text +"' " +
        //        " WHERE emp_cls_mast_id = '" + GridView11.DataKeys[e.RowIndex].Value + "'";
            
        //    //Conn.Open();
        //    //objCmd = new MySqlCommand(strSQL, Conn);
        //    //TextBox1.Text = strSQL;
        //    //TextBox2.Text = txtEditEmpClsSalHours.Text;
        //    objCmd = new MySqlCommand();
        //    objCmd.Connection = objConn;
        //    objCmd.CommandText = strSQL;
        //    objCmd.CommandType = CommandType.Text;

        //    objCmd.ExecuteNonQuery();
        //    //objCmd.ExecuteNonQuery();
            

        //    GridView11.EditIndex = -1;
        //    //GridView11.ShowFooter = true;
        //    BindData();
        //}
        //#endregion

        //void myGridView_RowCommand(Object source, GridViewCommandEventArgs e)
        //{
        //    if (e.CommandName == "Add")
        //    {
        //        //*** CustomerID ***//
        //        TextBox lblEmpClsMastHours = (TextBox)GridView11.FooterRow.FindControl("txtAddEmpClsMastHours");
        //        ////*** Email ***//
        //        //TextBox txtName = (TextBox)myGridView.FooterRow.FindControl("txtAddName");
        //        ////*** Name ***//
        //        //TextBox txtEmail = (TextBox)myGridView.FooterRow.FindControl("txtAddEmail");
        //        ////*** CountryCode ***//
        //        //TextBox txtCountryCode = (TextBox)myGridView.FooterRow.FindControl("txtAddCountryCode");
        //        ////*** Budget ***//
        //        //TextBox txtBudget = (TextBox)myGridView.FooterRow.FindControl("txtAddBudget");
        //        ////*** Used ***//
        //        //TextBox txtUsed = (TextBox)myGridView.FooterRow.FindControl("txtAddUsed");

        //        //strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " +
        //        //    " VALUES ('" + txtCustomerID.Text + "','" + txtName.Text + "','" + txtEmail.Text + "' " +
        //        //    " ,'" + txtCountryCode.Text + "','" + txtBudget.Text + "','" + txtUsed.Text + "') ";
        //        //objCmd = new OleDbCommand(strSQL, objConn);
        //        //objCmd.ExecuteNonQuery();

        //        //BindData();
        //    }
        //}

        //#region "Edit Button in order to Editing"
        //protected void modEditCommand(Object sender, GridViewEditEventArgs e)
        //{
        //    GridView11.EditIndex = e.NewEditIndex;
        //    //GridView11.ShowFooter = false;
        //    BindData();
        //}
        //#endregion

        //#region "Cancel for update gridview"
        //protected void modCancelCommand(Object sender, GridViewCancelEditEventArgs e)
        //{
        //    GridView11.EditIndex = -1;
        //    //GridView11.ShowFooter = true;
        //    BindData();
        //}
        //#endregion


        //#region "radiobuttonList"
        //protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    if (chkBox.SelectedValue == "0")
        //    {
        //        paySalary0.Enabled = false;
        //        paySalary1.Enabled = false;
        //        paySalary.Enabled = true;

        //    }
        //    else if (chkBox.SelectedValue == "1") 
        //    {
        //        paySalary.Enabled = false;
        //        paySalary1.Enabled = false;
        //        paySalary0.Enabled = true;

        //    }
        //    else if (chkBox.SelectedValue == "2") 
        //    {
        //        paySalary.Enabled = false;
        //        paySalary0.Enabled = false;
        //        paySalary1.Enabled = true;
        //    }
        //}
        //#endregion

        //#region "save value in pay_code_mast table"
        //void Save_PayCode()
        //{
        //    MySqlConnection Conn = new MySqlConnection(StrConn);
        //    string strSQL;
        //    strSQL = "INSERT INTO pay_code_mast (pay_code,pay_desc,pay_type,pay_salary)  " +
        //    " VALUES ('" + this.payCode.Text + "','" + this.payDesc.Text + "','" + this.payType.Text + "','" + this.paySalary.Text + "')";

        //    MySqlDataAdapter da4 = new MySqlDataAdapter(strSQL, Conn);
        //    Conn.Open();
        //    objCmd.Connection = Conn;
        //    objCmd.CommandType = CommandType.Text;
        //    objCmd.CommandText = strSQL;

        //    objCmd.ExecuteNonQuery();

        //    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), UniqueID, "alert('บันทึกข้อมูลเรียบร้อยแล้วค่ะ')", true);
        //}
        //#endregion

        #region "CMD_INSERT_Click"
        protected void CMD_INSERT_Click(object sender, EventArgs e)
        {
            MySqlConnection Conn = new MySqlConnection(StrConn);
            string strSQL;
            if (this.payHourAmt.Text.Equals(""))
            { this.payHourAmt.Text = "0"; }
            if (this.payMonthAmt.Text.Equals(""))
            { this.payMonthAmt.Text = "0"; }
            if (this.payTimeAmt.Text.Equals(""))
            { this.payTimeAmt.Text = "0"; }


            strSQL = "INSERT INTO pay_code_mast (pay_code,pay_type,pay_desc,pay_hour_amt,pay_month_amt,pay_time_amt,pay_rate,pay_rate1) " + //,pay_month_amt,pay_time_amt,pay_hour_amt,pay_rate,pay_rate1 )  " +
            " VALUES ('" + this.payCode.Text + "','"+this.pay_type.Text+"','" + this.payDesc.Text + "','" + Convert.ToDouble(this.payHourAmt.Text) + "','" + Convert.ToDouble(this.payMonthAmt.Text) + "','"+Convert.ToDouble(this.payTimeAmt.Text)+"','"+payRate.Text+"','"+payRate1.Text+"')";// "+
           // " ','" + Convert.ToDouble(this.payMonthAmt.Text) + "','" + Convert.ToDouble(this.payTimeAmt.Text) + "','" + Convert.ToDouble(this.payHourAmt.Text) + "','" + this.payRate.Text + "','" + this.payRate1.Text + "')";

            MySqlDataAdapter da4 = new MySqlDataAdapter(strSQL, Conn);
            Conn.Open();
            objCmd.Connection = Conn;
            objCmd.CommandType = CommandType.Text;
            objCmd.CommandText = strSQL;

            objCmd.ExecuteNonQuery();

            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), UniqueID, "alert('บันทึกข้อมูลเรียบร้อยแล้วค่ะ')", true);



            MySqlConnection Conn1 = new MySqlConnection(StrConn);
            string strSQL1;
            //if (this.payHourAmt.Text.Equals(""))
            //{ this.payHourAmt.Text = "0"; }
            //if (this.payMonthAmt.Text.Equals(""))
            //{ this.payMonthAmt.Text = "0"; }
            //if (this.payTimeAmt.Text.Equals(""))
            //{ this.payTimeAmt.Text = "0"; }


            strSQL1 = " update emp_mast SET " +
                      " pay_id = (select pcm.pay_id from pay_code_mast pcm where pcm.pay_code = '" + payCode.Text + "') " +

                      " WHERE emp_id = '" + Convert.ToInt32(Request.QueryString["emp_id"]) + "' "; 
                
                
                
                

            MySqlDataAdapter da5 = new MySqlDataAdapter(strSQL, Conn);
            Conn1.Open();
            objCmd.Connection = Conn1;
            objCmd.CommandType = CommandType.Text;
            objCmd.CommandText = strSQL1;

            objCmd.ExecuteNonQuery();

            BindData();

        }
        #endregion

        #region "retrive value to show in textboxs"
        protected void GridView12_SelectedIndexChanged(object sender, EventArgs e)
        {
            CMD_INSERT.Enabled = false;
            CMD_EDIT.Enabled = true;
            using (essystemEntities context = new essystemEntities())
            {
                List<pay_code_mast> crm1 = context.pay_code_mast.Where("it.[pay_id] = " + ((HiddenField)GridView12.SelectedRow.Cells[1].FindControl("HiddenField4")).Value.ToString() + "").ToList();
                if (crm1.Count > 0)
                {
                    pay_id.Text = crm1[0].pay_id.ToString();
                    payMonthAmt.Text = crm1[0].pay_month_amt.ToString();
                    payHourAmt.Text = crm1[0].pay_hour_amt.ToString();
                    payTimeAmt.Text = crm1[0].pay_time_amt.ToString();
                    payRate.Text = crm1[0].pay_rate;
                    payRate1.Text = crm1[0].pay_rate1;
                    //payTypeCheckBox.SelectedValue = crm1[0].pay_type.ToString();
                    if(crm1[0].pay_type.ToString()=="m"){
                        pay_type.Text = crm1[0].pay_type.ToString();
                        payTypeCheckBox.SelectedValue = crm1[0].pay_type.ToString();
                        payMonthAmt.Enabled = true;
                        payHourAmt.Enabled = false;
                        payTimeAmt.Enabled = false;
                        payRate.Enabled = false;
                        payRate1.Enabled = true;
                    }
                    else if (crm1[0].pay_type.ToString() == "t")
                    {
                        pay_type.Text = crm1[0].pay_type.ToString();
                        payTypeCheckBox.SelectedValue = crm1[0].pay_type.ToString();
                        payMonthAmt.Enabled = false;
                        payHourAmt.Enabled = false;
                        payTimeAmt.Enabled = true;
                        payRate.Enabled = false;
                        payRate1.Enabled = false;
                    }
                    else if (crm1[0].pay_type.ToString() == "h")
                    {
                        pay_type.Text = crm1[0].pay_type.ToString();
                        payTypeCheckBox.SelectedValue = crm1[0].pay_type.ToString();
                        payMonthAmt.Enabled = false;
                        payHourAmt.Enabled = true;
                        payTimeAmt.Enabled = false;
                        payRate.Enabled = false;
                        payRate1.Enabled = false;
                    }
                    else if (crm1[0].pay_type.ToString() == "r")
                    {
                        pay_type.Text = crm1[0].pay_type.ToString();
                        payTypeCheckBox.SelectedValue = crm1[0].pay_type.ToString();
                        payMonthAmt.Enabled = false;
                        payHourAmt.Enabled = false;
                        payTimeAmt.Enabled = false;
                        payRate.Enabled = false;
                        payRate1.Enabled = true;
                    }
                    else if (crm1[0].pay_type.ToString() == "p")
                    {
                        //int i = 0;
                        //for (i = 0; i <= (int)this.payTypeCheckBox.Items.Count - 1; i++)
                        //{
                        //    if (this.payTypeCheckBox.Items[i].Value == "m")
                        //    {
                        //        this.payTypeCheckBox.Items[i].Selected = true;
                        //    }
                        //    if (this.payTypeCheckBox.Items[i].Value == "r")
                        //    {
                        //        this.payTypeCheckBox.Items[i].Selected = true;
                        //    }
                            

                        //}

                        pay_type.Text = crm1[0].pay_type.ToString();
                        this.payTypeCheckBox.Items[0].Selected = true;
                        this.payTypeCheckBox.Items[3].Selected = true;
                        payMonthAmt.Enabled = true;
                        payHourAmt.Enabled = false;
                        payTimeAmt.Enabled = false;
                        payRate.Enabled = true;
                        payRate1.Enabled = true;
                    }
                    //payDate.Text = crm1[0].pay_date.ToString();
                    payDesc.Text = crm1[0].pay_desc;
                    payCode.Text = crm1[0].pay_code;
                    CMD_INSERT.Enabled = false;
                    CMD_EDIT.Enabled = true;
                    //emp_id.Text = crm1[0].emp_id.ToString();
                }
            }
        }
        #endregion

        #region "show popup after deleting value"
        protected void GridView12_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{

            //    ImageButton ib = (ImageButton)e.Row.FindControl("ImageButton1");
            //    ib.Attributes.Add("onclick", "return confirm('คุณต้องการที่จะลบข้อมูล?');");

            //}
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // loop all data rows
                foreach (DataControlFieldCell cell in e.Row.Cells)
                {
                    // check all cells in one row
                    foreach (Control control in cell.Controls)
                    {
                        // Must use LinkButton here instead of ImageButton
                        // if you are having Links (not images) as the command button.
                        ImageButton button = control as ImageButton;
                        if (button != null && button.CommandName == "Delete")
                            // Add delete confirmation
                            button.OnClientClick = "if (!confirm('คุณต้องการที่จะลบข้อมูล?')) return;";
                    }
                }
            }
        

            //Label lblCustomerID = (Label)(e.Row.FindControl("lblCustomerID"));
            //if (lblCustomerID != null)
            //{
            //    lblCustomerID.Text = (string)DataBinder.Eval(e.Row.DataItem, "CustomerID");
            //}
        }
        #endregion

        #region "delete value form gridview"
        protected void GridView12_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {


            MySqlConnection Conn = new MySqlConnection(StrConn);
            string strSQL;
            strSQL = "DELETE FROM pay_code_mast WHERE pay_id ='" + GridView12.DataKeys[e.RowIndex].Value + "'";
            Conn.Open();
            objCmd = new MySqlCommand(strSQL, Conn);

            objCmd.ExecuteNonQuery();



            
            MySqlConnection Conn1 = new MySqlConnection(StrConn);
            string strSQL1;
            pay_id.Text = "";


            strSQL1 = "UPDATE emp_mast SET " +
        " pay_id = '0' " +

        " WHERE emp_id = '" + Convert.ToInt32(requestEmpId.Text)  +"' ";
            MySqlDataAdapter da4 = new MySqlDataAdapter(strSQL1, Conn1);
            Conn1.Open();
            objCmd.Connection = Conn1;
            objCmd.CommandType = CommandType.Text;
            objCmd.CommandText = strSQL1;

            objCmd.ExecuteNonQuery();



            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "Alert3", "alert('ลบข้อมูลเรียบร้อยแล้ว')", true);

            
            GridView12.EditIndex = -1;
            clear();
            CMD_INSERT.Enabled = true;
            CMD_EDIT.Enabled = false;
            getData();
            
            
        }
        #endregion

        #region "getData after delete pay row"
        void getData()
        {

            MySqlConnection cnx = new MySqlConnection(StrConn);
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            string cmdText = "SELECT * FROM pay_code_mast where pay_id ='" + pay_id.Text + "' " ;
            MySqlCommand cmd = new MySqlCommand(cmdText, cnx);
            DataSet ds = new DataSet();
            adapter.SelectCommand = cmd;
            adapter.Fill(ds);
            GridView12.DataSource = ds;
            GridView12.DataBind();
        }
        #endregion

        protected void empId_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            int i = 0;
            payDesc.Text = "";
            checkStatus.Text = "";
            for (i = 0; i <= (int)this.payTypeCheckBox.Items.Count - 1; i++)
            {

                if (this.payTypeCheckBox.Items[i].Selected == true)
                {
                    checkStatus.Text = checkStatus.Text + this.payTypeCheckBox.Items[i].Value;
                    if (checkStatus.Text.Equals("mr") || checkStatus.Text.Equals("rm"))
                    {
                        pay_type.Text = "p";
                        payDesc.Text = "รายเดือน และ %ค่าลงทะเบียน ";
                        payMonthAmt.Enabled = true;
                        payHourAmt.Enabled = false;
                        payTimeAmt.Enabled = false;
                        payRate.Enabled = true;
                        payRate1.Enabled = true;
                        
                        payHourAmt.Text = "";
                        payTimeAmt.Text = "";
                        
                       
                        //if (payDesc.Text == "")
                        //{
                        //    payDesc.Text = this.empIdCheckBox.Items[i].Text;
                        //}
                        //else
                        //{

                        //    payDesc.Text = payDesc.Text + " และ " + this.empIdCheckBox.Items[i].Text;
                        //}
                    }
                    else if (checkStatus.Text.Equals("m"))
                    {
                        pay_type.Text = "m";
                        payDesc.Text = "รายเดือน";
                        payMonthAmt.Enabled = true;
                        payHourAmt.Enabled = false;
                        payTimeAmt.Enabled = false;
                        payRate.Enabled = false;
                        payRate1.Enabled = true;
                        
                        payHourAmt.Text = "";
                        payTimeAmt.Text = "";
                        
                        payRate1.Text = "";
                    }
                    else if (checkStatus.Text.Equals("t"))
                    {
                        pay_type.Text = "t";
                        payDesc.Text = "รายครั้ง";
                        payMonthAmt.Enabled = false;
                        payHourAmt.Enabled = false;
                        payTimeAmt.Enabled = true;
                        payRate.Enabled = false;
                        payRate1.Enabled = false;
                        payMonthAmt.Text = "";
                        payHourAmt.Text = "";
                        
                        payRate.Text = "";
                        payRate1.Text = "";
                    }
                    else if (checkStatus.Text.Equals("h"))
                    {
                        pay_type.Text = "h";
                        payDesc.Text = "รายชั่วโมง";
                        payMonthAmt.Enabled = false;
                        payHourAmt.Enabled = true;
                        payTimeAmt.Enabled = false;
                        payRate.Enabled = false;
                        payRate1.Enabled = false;
                        payMonthAmt.Text = "";
                        
                        payTimeAmt.Text = "";
                        payRate.Text = "";
                        payRate1.Text = "";
                    }
                    else if (checkStatus.Text.Equals("r"))
                    {
                        pay_type.Text = "r";
                        payDesc.Text = "% ค่าลงทะเบียน";
                        payMonthAmt.Enabled = false;
                        payHourAmt.Enabled = false;
                        payTimeAmt.Enabled = false;
                        payRate.Enabled = true;
                        payRate1.Enabled = false;
                        payMonthAmt.Text = "";
                        payHourAmt.Text = "";
                        payTimeAmt.Text = "";
                        payRate.Text = "";
                        
                    }
                    else 
                    {
                        ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "Alert7", "alert('ไม่สามารถเลือกรูปแบบนี้ได้')", true);
                        this.payTypeCheckBox.ClearSelection();
                        payDesc.Text = "";
                        checkStatus.Text = "";
                        payMonthAmt.Text = "";
                        payHourAmt.Text = "";
                        payTimeAmt.Text = "";
                        payRate.Text = "";
                        payRate1.Text = "";
                        payCode.Text = "";
                        payMonthAmt.Enabled = true;
                        payHourAmt.Enabled = true;
                        payTimeAmt.Enabled = true;
                        payRate.Enabled = true;
                        payRate1.Enabled = true;
                    }
                }
            }
            //if (empIdCheckBox.SelectedIndex == 0) {
            //    if (empIdCheckBox.SelectedIndex == 3)
            //    {
            //        payDesc.Text = "จ่ายแบบรายเดือน + %ค่าลงทะเบียน";
            //    }
            //    else 
            //    {
            //        payDesc.Text = "จ่ายแบบรายเดือน";
            //    }
            //}
            //else if(empIdCheckBox.SelectedIndex == 1){
            //    payDesc.Text = "จ่ายแบบรายครั้ง";
            //}
            //else if (empIdCheckBox.SelectedIndex == 2)
            //{
            //    payDesc.Text = "จ่ายแบบรายชั่วโมง";
            //}
            //else if (empIdCheckBox.SelectedIndex == 3)
            //{
            //    payDesc.Text = "% ค่าลงทะเบียน";
            //}
            //else if (empIdCheckBox.SelectedIndex == 0 && empIdCheckBox.SelectedIndex == 3)
            //{
            //    payDesc.Text = "จ่ายแบบรายเดือน + %ค่าลงทะเบียน";
            //}
            //else {
            //    
            //}
            
        }

        //#region "dateShow in payDate"
        //protected void dateShow(object sender, EventArgs e)
        //{

        //    payDate.Text = string.Format("{0:dd/MM/yyyy}", DateTime.Now).ToString();
        //}
        //#endregion

        //#region "showYear in year"
        //protected void showYear(object sender, EventArgs e)
        //{

        //    for_year.Text = string.Format("{0:yyyy}", DateTime.Now).ToString();
        //}
        //#endregion

        //#region "calulate sum month,day,hours"
        //protected void Button1_Click(object sender, EventArgs e) 
        //{
        //    CheckBox chkCusID;
        //    Label lblID1;
        //    Label lblID2;
        //    Label lblID3;
        //    int i;
        //    double month = 0;
        //    double day = 0;
        //    double hours = 0;
        //    //lblText.Text = "";
        //    for (i = 0; i <= GridView11.Rows.Count - 1; i++)
        //    {
        //        chkCusID = (CheckBox)GridView11.Rows[i].FindControl("chkBox");
        //        lblID1 = (Label)GridView11.Rows[i].FindControl("emp_cls_sal_month");
        //        lblID2 =  (Label)GridView11.Rows[i].FindControl("emp_cls_sal_day");
        //        lblID3 = (Label)GridView11.Rows[i].FindControl("emp_cls_sal_hours");
        //        if (chkCusID.Checked)
        //        {

        //            month = (month + Convert.ToDouble(lblID1.Text));
        //            day = (day + Convert.ToDouble(lblID2.Text));
        //            hours = (hours + Convert.ToDouble(lblID3.Text));

        //           //lblID1.Text = "0";
        //           //lblID2.Text = "0";
        //           //lblID3.Text = "0";
        //        }
        //    }

        //    paySalary.Text = String.Format("{0:0.00}", month);
        //    paySalary0.Text = String.Format("{0:0.00}", day);
        //    paySalary1.Text = String.Format("{0:0.00}", hours);    
        //}
        //#endregion

        
        //void DropDownListAddInsertItem()
        //{
        //    //SortedList mySortedList = new SortedList();

        //    //mySortedList.Add("M", "Man");
        //    //mySortedList.Add("W", "Woman");

        //    ////*** DropDownList ***//
        //    //this.myDDL4.DataSource = mySortedList;
        //    //this.myDDL4.DataTextField = "Value";
        //    //this.myDDL4.DataValueField = "Key";
        //    //this.myDDL4.DataBind();


        //    //*** Add & Insert New Item ***//
        //    int strText = 2555;
        //    int strValue = 2555;

        //    //*** Insert Item ***//
            
        //    int i ;
        //    for (i = 2555; i <= 2575 - 1; i++)
        //    {
        //        ListItem InsertItem = new ListItem( i.ToString(), i.ToString());
        //        DropDownList1.Items.Insert(0, InsertItem);
        //    }
        //    //*** Add Items ***//		
        //    //strText = "Guy";
        //    //strValue = "G";
        //    //ListItem AddItem = new ListItem(strText, strValue);
        //    //myDDL4.Items.Add(AddItem);

        //    ////*** Default Value ***//
        //    //myDDL4.SelectedIndex = myDDL4.Items.IndexOf(myDDL4.Items.FindByValue(""));  //*** By DataValueField ***//
        //    //myDDL4.SelectedIndex = myDDL4.Items.IndexOf(myDDL4.Items.FindByText(""));  //*** By DataTextField ***//
        //}
        #region "CMD_EDIT_Click"
        protected void CMD_EDIT_Click(object sender, EventArgs e)
        {

            MySqlConnection Conn = new MySqlConnection(StrConn);
            string strSQL;
            if (this.payHourAmt.Text.Equals(""))
            { this.payHourAmt.Text = "0"; }
            if (this.payMonthAmt.Text.Equals(""))
            { this.payMonthAmt.Text = "0"; }
            if (this.payTimeAmt.Text.Equals(""))
            { this.payTimeAmt.Text = "0"; }


            strSQL = "UPDATE pay_code_mast SET " +
        " pay_code = '" + this.payCode.Text + "' " +
        " ,pay_desc = '" + this.payDesc.Text + "' " +
         " ,pay_month_amt = '" + this.payMonthAmt.Text + "' " +
          " ,pay_time_amt = '" + this.payTimeAmt.Text + "' " +
           " ,pay_hour_amt = '" + this.payHourAmt.Text + "' " +
            " ,pay_rate = '" + this.payRate.Text + "' " +
              " ,pay_rate1 = '" + this.payRate1.Text + "' " +
              " ,pay_type = '" + this.pay_type.Text + "' " +
        " WHERE pay_id = '" + Convert.ToInt32(this.pay_id.Text)+ "' ";
            MySqlDataAdapter da4 = new MySqlDataAdapter(strSQL, Conn);
            Conn.Open();
            objCmd.Connection = Conn;
            objCmd.CommandType = CommandType.Text;
            objCmd.CommandText = strSQL;

            objCmd.ExecuteNonQuery();

            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), UniqueID, "alert('บันทึกข้อมูลเรียบร้อยแล้วค่ะ')", true);



            //MySqlConnection Conn1 = new MySqlConnection(StrConn);
            //string strSQL1;
            //if (this.payHourAmt.Text.Equals(""))
            //{ this.payHourAmt.Text = "0"; }
            //if (this.payMonthAmt.Text.Equals(""))
            //{ this.payMonthAmt.Text = "0"; }
            //if (this.payTimeAmt.Text.Equals(""))
            //{ this.payTimeAmt.Text = "0"; }


            //strSQL1 = " update emp_mast SET " +
            //          //" pay_id = (select pcm.pay_id from pay_code_mast pcm where pcm.pay_code = '" + payCode.Text + "') " +
            //          " pay_id = '' " +
            //          " WHERE emp_id = '" + Convert.ToInt32(Request.QueryString["emp_id"]) + "' ";





            //MySqlDataAdapter da5 = new MySqlDataAdapter(strSQL, Conn);
            //Conn1.Open();
            //objCmd.Connection = Conn1;
            //objCmd.CommandType = CommandType.Text;
            //objCmd.CommandText = strSQL1;

            //objCmd.ExecuteNonQuery();

            BindData();

        }
        #endregion
    }
    
}